﻿using System;

namespace SmartCard.Framework
{
	public class ApduResponse
	{
		#region properties

		public byte Sw1
		{ get; private set; }

		public byte Sw2
		{ get; private set; }

		public byte[] Data
		{ get; private set; }

		public eApduResponseMessage ResponseMessage
		{ get; private set; }

		#endregion

		#region construction

		public ApduResponse(byte[] response)
			: this(response,
			(ushort) ((response == null) ? 0 : response.Length))
		{ }

		public ApduResponse(byte[] response, ushort responseLength)
		{
			if (response == null)
			{ throw new ArgumentNullException("response"); }

			if (responseLength < 2)
			{ throw new ArgumentException("responseLength", "response length must be 2 bytes or more"); }

			if (response.Length < responseLength)
			{ throw new ArgumentOutOfRangeException("value can not be larger than length of array", "responseLength"); }

			Sw1 = response[responseLength - 2];
			Sw2 = response[responseLength - 1];
			Data = new byte[responseLength - 2];
			Array.Copy(response, Data, Data.Length);
		}

		#endregion
	}
}
